<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "goods".
 *
 * @property string $id
 * @property string $name
 * @property string $sn
 * @property integer $goods_category_id
 * @property integer $brand_id
 * @property integer $supplier_id
 * @property integer $stock_type
 * @property integer $goods_type_id
 * @property string $shop_price
 * @property string $market_price
 * @property integer $stock
 * @property integer $is_on_sale
 * @property string $goods_status
 * @property string $keyword
 * @property string $logo
 * @property integer $status
 * @property integer $sort
 */
class Goods extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'goods';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_category_id', 'brand_id', 'supplier_id', 'stock_type', 'goods_type_id', 'stock', 'is_on_sale', 'goods_status', 'status', 'sort'], 'integer'],
            [['is_on_sale'],'required'],
            [['shop_price', 'market_price'], 'number'],
            [['name', 'sn'], 'string', 'max' => 50],
            [['keyword'], 'string', 'max' => 20],
            [['logo'], 'string', 'max' => 100]
        ];
    }


    /**
     * 添加一条新的商品记录
     * @param $data 表单数据
     */
    public function addGoodsInfo($goodsData)
    {
//        echo '<pre>';
//        var_dump($goodsData);
//        exit;
        //>>1,处理goods_status数据
        if(!$goodsData['Goods']['goods_status']){
            $goodsData['Goods']['goods_status']=0;
        }else{
            $goods_status=0;
            foreach($goodsData['Goods']['goods_status'] as $statu){
                $goods_status = $goods_status|$statu;
            }
            $goodsData['Goods']['goods_status']=$goods_status;
        }
        //2,将$goodsData数据传入数据模型并保存
        if($this->load($goodsData) && $this->save()){
            $id=Yii::$app->db->lastInsertID;
        }else{
            return false;
        }
        //3,将$goodsData['goods_intro']数据添加到goods_intro表中
        $goodsIntroModel=new GoodsIntro();
        if(!$goodsIntroModel->addGoodsIntro($id,$goodsData['goods_intro'])){
            echo '失败';
        }
        echo '成功';
        //4,


    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '商品序号',
            'name' => '商品名字',
            'sn' => '商品货号',
            'goods_category_id' => 'Goods Category ID',
            'brand_id' => 'Brand ID',
            'supplier_id' => 'Supplier ID',
            'stock_type' => 'Stock Type',
            'goods_type_id' => 'Goods Type ID',
            'shop_price' => 'Shop Price',
            'market_price' => 'Market Price',
            'stock' => 'Stock',
            'is_on_sale' => 'Is On Sale',
            'goods_status' => 'Goods Status',
            'keyword' => 'Keyword',
            'logo' => 'Logo',
            'status' => 'Status',
            'sort' => 'Sort',
        ];
    }

}
